var audio = cc.Class({
   
    init(){
        this.audioData = {};
        this.preloadMusic();
    },

    preloadMusic(){
        this.onLodeAudioRes("music", this.saveGameAudio.bind(this), "start");
    },
    
    playGameAudio:function(name){
        if(name != "music")
        {
            if(window.gameConfig.gameData.isSound != 1)return
        }
        else
        {
            if(window.gameConfig.gameData.isMusic != 1)return
        }

        if(this.audioData[name])
        {
            //不是背景音乐
            if(name.indexOf("music") != -1)
            {
                console.log("musicState: "+cc.audioEngine.isLoop(this.gameMusicId));
                //是否是循环状态  是---return; 否 ---- 播放
                if(cc.audioEngine.isLoop(this.gameMusicId))return;
                this.gameMusicId = cc.audioEngine.play(this.audioData[name]);
                cc.audioEngine.setVolume(this.gameMusicId, 0.7);
                cc.audioEngine.setLoop(this.gameMusicId, true);
            }
            else
            {
                cc.audioEngine.play(this.audioData[name]);
            }
        }
        else
        {
            this.onLodeAudioRes(name,this.saveGameAudio.bind(this));
        }
    },

    stopGameAudio:function(name){
        cc.audioEngine.stopAll(this.gameMusicId);
    },

    saveGameAudio:function(audio, audioName, type = "play"){
        this.audioData[audioName] = audio;
        if(type != "start")this.playGameAudio(audioName);
    },

    onLodeAudioRes:function(audioName, callback, type){
        cc.loader.loadRes("audio/" + audioName, cc.AudioEnging, function (err, audio) {
            if(err == null)callback(audio,audioName, type);
        });
    },
});
